﻿<%

class BlogCatClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "BLOGCAT"
    end property

    public blogId, catId, catName 
    private m_pageSize
    private m_totalItemCount, m_itemIDsInPage
    private m_topItemIDs
    private m_topComments
    private m_isRSS
    private m_rssURL
    private m_rssReloading
    
    
    public property let pageSize(page_size)
        m_pageSize = page_size
    end property
    
    public property get pageSize
        if isEmpty(m_pageSize) then
            dim blog
            set blog = new BlogClass
            blog.load(blogId)
            m_pageSize = blog.pageSize
        end if  
        pageSize = m_pageSize
    end property
    
    public property get isRSS
        isRSS = m_isRSS
    end property
    
    
    private sub class_initialize()
    end sub
    
    private sub class_terminate()
    end sub
      
    private function build(array)
        build = false
        if isEmpty(array) or not isArray(array) then
            exit function
        end if
        if ubound(array)<8 then
            exit function
        end if

        'response.write "size="&
        blogId      = array(0)
        catId       = array(1)
        if isNull(catId) or catId="null" then
            catId = "null"
        elseif isEmpty(catId) or catId="" then
            catId = ""
        else
            catId = clng(catId)    
        end if
        catName     = array(2)   
        m_pageSize  = array(3)   
        m_totalItemCount= array(4)
        m_itemIDsInPage = array(5)
        m_isRSS = array(6)
        m_rssURL = array(7)

        build = true 
    end function 
    
    private function toArray()
       dim array(8)
       array(0) = blogId
       array(1) = catId
       array(2) = catName
       array(3) = m_pageSize 
       array(4) = m_totalItemCount
       array(5) = m_itemIDsInPage
       array(6) = m_isRSS
       array(7) = m_rssURL
       
       toArray = array     
    end function
    
    public function load(blog_id, cat_id)
        load = false
        blogId = clng(blog_id)
        
        catId = cat_id
        if isNull(catId) or catId="null" then '未归类
            catId = "null"
        elseif isEmpty(catId) or catId="" then '全部
            catId = ""
        else
            catId = clng(catId)    
        end if

        load = loadFromCache
        
        if load=false then
            load = loadFromDB()
            if (load=true) then
                call cache()
            end if    
        end if     
    end function
    
    public function reload(blog_id, cat_id)
        reload = false
        
        blogId = clng(blog_id)
        catId = cat_id
        if isNull(catId) or catId="null" then '未归类
            catId = "null"
        elseif isEmpty(catId) or catId="" then '全部
            catId = ""
        else
            catId = clng(catId)    
        end if
        
        reload = loadFromDB()
        if reload=true then
            call cache()
        else    
            call clean()
        end if    
    end function
        
    public function clean()
        call removeFromCache(CACHE_PREFIX, blogId & "." & catId)
        blogId      = Empty
        catId       = Empty
        catName     = Empty   
        m_pageSize  = Empty   
        m_totalItemCount= Empty
        m_itemIDsInPage = Empty

    end function

    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, blogId & "." & catId))
    end function
    
    public sub cache()
       call build(putIntoCache(CACHE_PREFIX, blogId & "." & catId, toArray()))
    end sub
    

    
    private function loadFromDB()
        'response.write "loadFromDB"
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        call openDB
      
      loadFromDB = false
      if catId="null" or isNull(catId) then
        catName = "未归类"
      elseif isEmpty(catId) or catId="" then
        catName = "" '全部文章
      else
        sql = " select cat_name,is_rss,rss_url from blog_category where blog_id=" & blogId & " and cat_id=" & catId
        rs.open sql, conn, 1, 1
        if not rs.eof then
          catName = rs("cat_name")
          m_isRSS = rs("is_rss")
          m_rssURL = rs("rss_url")
          loadFromDB = true
        else
          catName = ""
        end if
        rs.close
      end if
      set rs = nothing
      
      
      if m_isRSS then
            call loadFromRSS
      else
          call loadTotalItemCountFromDB()
          if m_totalItemCount>0 then
               call loadBlogItemsFromDB(1)
          end if    
      end if
      loadFromDB = true
    end function  
    
    private sub loadFromRSS()
        dim RSS
        Set RSS = new kwRSS_reader
            RSS.ParseLocation(m_rssURL)
            'catName = RSS.ChannelTitle
            m_totalItemCount = RSS.GetTotalItems
            m_pageSize = m_totalItemCount
        redim m_itemIDsInPage(1)
    
        dim itemIDs
        redim itemIDs(m_totalItemCount)
        
        dim item
        dim i
        i = 0
            while not RSS.EOF
    
                Set item = new BlogItemClass
                item.isRSS = True
                
                item.blogId = blogId
                item.catId = catId
                item.catName = catName
                
                item.itemId = -1 * (catId * 1000 + i)
                itemIDs(i) = item.itemId
                
                item.title = RSS.GetTitle
                item.detail = RSS.GetDesc & "<p><i>本文从<a href=" & RSS.ChannelURL & ">" & RSS.ChannelTitle & "</a>自动获取，<a href=" & RSS.getLink & " target=_rss>阅读原文</a></i></p>"
                item.userName = RSS.GetAuthor
                item.webUrl = RSS.getLink
                
                dim pubDate

                pubDate = StringToDate(RSS.getPubDate)
                item.startDate = pubDate
                item.EndDate = pubDate
                item.insertTime = pubDate
                item.updateTime = pubDate
                item.isSecret = false
                item.replyCount = 0
                item.trackCount = 0
                item.showContent = true
                
                item.cache()
                
                i = i + 1
                RSS.MoveNext
            wend
            
            m_itemIDsInPage(0) = itemIDs
            
            Set RSS = nothing
    
        end sub
    
    public sub loadTotalItemCountFromDB()
        'response.write(" loadTotalPageCountFromDB" )
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        call openDB

        sql = " select count(content_id) from blog_content where blog_id=" & blogId
        
        if isNull(catId) or catId="null" then
            sql = sql & " and cat_id is null "
        elseif not isEmpty(catId) and catId<>"" then
            sql = sql & " and cat_id=" & catId
        end if

        rs.open sql,conn,1,1
        m_totalItemCount = rs(0)
            
        redim m_itemIDsInPage(totalPageCount)
        rs.close
        set rs = nothing

    end sub
    
    public property get totalItemCount
        'if isEmpty(m_totalItemCount) then
        '    call loadTotalItemCountFromDB()
        '    call cache()
        'end if    
        totalItemCount = m_totalItemCount
    end property
    
    public property get totalPageCount
        'if isEmpty(m_totalItemCount) then
        '    call loadTotalItemCountFromDB()
        '    call cache()
        'end if    
      if m_isRSS then
          totalPageCount = 1
      else          
        if pageSize <=0 then
            totalPageCount=0
        else
            totalPageCount = (m_totalItemCount + pageSize -1)\pageSize
        end if    
      end if
    end property

    
    private sub loadBlogItemsFromDB(pageNo)
        'response.write(" loadBlogItemsFromDB:" & pageNo)
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB

        if isEmpty(pageNo) or pageNo="" then
            pageNo = 1
        end if
        pageNo = clng(pageNo)
        if pageNo<1 or pageNo>totalPageCount then
            pageNo=1
        end if
        
        dim itemIDs
        redim itemIDs(pageSize)
        

        if (pageSize>0 and pageNo>0) then
            sql = " select top " & (pageNo * pageSize) & " content_id from blog_content where blog_id=" & blogID 

            if isNull(catId) or catId="null"  then
                sql = sql & " and cat_id is null "
            elseif not isEmpty(catId) and catId<>"" then
                sql = sql & " and cat_id=" & catId
            end if
            
            sql = sql & "  order by end_date desc,start_date desc ,insert_time desc"
            
            
            rs.open sql,conn,1,1
            if not rs.eof then
                rs.pageSize=pageSize
                rs.absolutePage=pageNo
                dim i, item_id
                i=0
                set item_id = rs("content_id")
                do while not rs.eof and i<ubound(itemIDs)
                    itemIDs(i) = item_id
                    rs.moveNext()
                    i = i+1
                loop    
            end if
            rs.close
        end if        
        
        set rs = nothing
        m_itemIDsInPage(pageNo-1) = itemIDs
        
    end sub
    
    private function getRSSItems(pageNo)
        if isEmpty(m_itemIDsInPage(pageNo-1)) or not isArray(m_itemIDsInPage(pageNo-1))  then
            loadFromRSS()
            call cache()
        end if
        
        dim itemIDs , count, i
        itemIDs = m_itemIDsInPage(pageNo-1)
        count = ubound(itemIDs)
        
        if count<1 then
            exit function
        end if
        
        dim items, item
        redim items(count)
        
        for i=0 to count-1
            set item = new BlogItemClass
            dim loadResult
            loadResult = item.load(itemIDs(i))
            
            '如果文章加载失败说明已从缓存中清除，需要重新加载RSS并缓存
            'm_rssReloading用户控制仅递归一次
            if loadResult=False and m_rssReloading=False then
            	m_rssReloading = True
              loadFromRSS()
              call cache()
            	getRSSItems = getRSSItems(pageNo)
            	m_rssReloading = False
            	exit function
            end if
            
            set items(i) = item
        next
        
        getRSSItems = items    
    end function


    public function getBlogItems(pageNo)
        if pageNo<1 or pageNo>totalPageCount then
            exit function
        end if
        
        if m_isRSS then
            getBlogItems = getRSSItems(pageNo)
            exit function
            
        elseif isEmpty(m_itemIDsInPage(pageNo-1)) or not isArray(m_itemIDsInPage(pageNo-1))  then
            loadBlogItemsFromDB(pageNo)
            call cache()
        end if
        
        dim itemIDs , count, i
        itemIDs = m_itemIDsInPage(pageNo-1)
        count = ubound(itemIDs)
        
        if count<1 then
            exit function
        end if
        
        dim items, item
        redim items(count)
        
        for i=0 to count-1
            set item = new BlogItemClass
            item.load(itemIDs(i))
            set items(i) = item
        next
        
        getBlogItems = items
    end function

    public function checkUserRight(user_id)
        set checkUserRight = new UserRightClass
        call checkUserRight.load(user_id, blogId, catId) 
    end function
        
end class

%>